package com.mmy.controller.line;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;
import com.mmy.domain.line.TLineOrder;
import com.mmy.domain.line.VLineBase;
import com.mmy.domain.line.VLineOrder;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.impl.constzone.ConstZoneServiceImpl;
import com.mmy.service.impl.line.LineBaseServiceImpl;
import com.mmy.service.impl.line.LineOrderServiceImpl;
import com.mmy.service.impl.line.LinePlanServiceImpl;
import com.mmy.service.impl.line.LineTripServiceImpl;

@Controller
public class LineOrderController {

	@Autowired
	LineOrderServiceImpl lineOrderServiceImpl;
	@Autowired
	LineBaseServiceImpl lineBaseServiceImpl;
	@Autowired
	LineTripServiceImpl lineTripServiceImpl;
	@Autowired
	LinePlanServiceImpl linePlanServiceImpl;
	@Autowired
	ConstZoneServiceImpl constZoneServiceImpl;

	private static Logger logger = LoggerFactory.getLogger(LineOrderController.class);

	/**
	 * 函数功能说明： 跳转--预留单选项卡
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人liys
	 * @date 创建日期 2020.12.24
	 */
	@RequestMapping("orderreserved")
	public String orderReserved(Model model, HttpSession session, HttpServletRequest request, String pageNumber,
			String pageSize) {

		int iType = Integer.parseInt(request.getParameter("type"));
		model.addAttribute("type", iType);
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		String orderType = request.getParameter("orderType");
		String lineName = request.getParameter("lineName");
		String strReserveName = request.getParameter("reserveName");
		String strReserveTel = request.getParameter("reserveTel");
		String strReserveDay = request.getParameter("reserveDay");
		List<VLineOrder> lstLineOrder = lineOrderServiceImpl.reserved(tSysUser, orderType, lineName, strReserveName,
				strReserveTel, strReserveDay, pageNumber, pageSize, iType);
		model.addAttribute("lstLineOrder", lstLineOrder);
		Map<String, Integer> mapPage = lineOrderServiceImpl.reservedPage(tSysUser.getOrgID(), pageNumber, pageSize,
				iType);
		model.addAttribute("orderType", orderType);
		model.addAttribute("lineName", lineName);
		model.addAttribute("name", strReserveName);
		model.addAttribute("tel", strReserveTel);
		model.addAttribute("date", strReserveDay);
		model.addAttribute("iPageNo", mapPage.get("iPageNo"));
		model.addAttribute("iTotalCount", mapPage.get("iTotalCount"));
		model.addAttribute("iMaxPage", mapPage.get("iMaxPage"));
		model.addAttribute("iPageSize", mapPage.get("iPageSize"));
		return "webbackground/tourist/OrderReserved";
	}

	/**
	 * 函数功能说明： 跳转--订单选项卡
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人liys
	 * @date 创建日期 2020.12.24
	 */
	@RequestMapping("orderpaid")
	public String orderPaid(Model model, HttpSession session, HttpServletRequest request, String pageNumber,
			String pageSize) {

		int iType = Integer.parseInt(request.getParameter("type"));
		model.addAttribute("type", iType);
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		String orderType = request.getParameter("orderType");
		String lineName = request.getParameter("lineName");
		String strOrderName = request.getParameter("orderName");
		String strOrderTel = request.getParameter("orderTel");
		String strOrderDay = request.getParameter("orderDay");
		List<VLineOrder> lstLineOrder = lineOrderServiceImpl.paid(tSysUser, orderType, lineName, strOrderName,
				strOrderTel, strOrderDay, pageNumber, pageSize, iType);
		model.addAttribute("lstLineOrder", lstLineOrder);
		Integer iOrgID = tSysUser.getOrgID();
		Map<String, Integer> mapPage = lineOrderServiceImpl.paidPage(iOrgID, pageNumber, pageSize, iType);
		model.addAttribute("orderType", orderType);
		model.addAttribute("lineName", lineName);
		model.addAttribute("name", strOrderName);
		model.addAttribute("tel", strOrderTel);
		model.addAttribute("date", strOrderDay);
		model.addAttribute("iPageNo", mapPage.get("iPageNo"));
		model.addAttribute("iTotalCount", mapPage.get("iTotalCount"));
		model.addAttribute("iMaxPage", mapPage.get("iMaxPage"));
		model.addAttribute("iPageSize", mapPage.get("iPageSize"));
		return "webbackground/tourist/OrderPaid";
	}

	/**
	 * 函数功能说明： 退单选项卡
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人liys
	 * @date 创建日期 2020.12.28
	 */
	@RequestMapping("/ordercancel")
	public String orderCancel(Model model, HttpSession session, HttpServletRequest request, String pageNumber,
			String pageSize) {

		int iType = Integer.parseInt(request.getParameter("type"));
		model.addAttribute("type", iType);
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		String orderType = request.getParameter("orderType");
		String lineName = request.getParameter("lineName");
		String strOrderName = request.getParameter("orderName");
		String strOrderTel = request.getParameter("orderTel");
		String strOrderDay = request.getParameter("orderDay");
		List<VLineOrder> lstLineOrder = lineOrderServiceImpl.cancel(tSysUser, orderType, lineName, strOrderName,
				strOrderTel, strOrderDay, pageNumber, pageSize, iType);
		model.addAttribute("lstLineOrder", lstLineOrder);
		Map<String, Integer> mapPage = lineOrderServiceImpl.cancelPage(tSysUser.getOrgID(), pageNumber, pageSize,
				iType);
		model.addAttribute("orderType", orderType);
		model.addAttribute("lineName", lineName);
		model.addAttribute("name", strOrderName);
		model.addAttribute("tel", strOrderTel);
		model.addAttribute("date", strOrderDay);
		model.addAttribute("iPageNo", mapPage.get("iPageNo"));
		model.addAttribute("iTotalCount", mapPage.get("iTotalCount"));
		model.addAttribute("iMaxPage", mapPage.get("iMaxPage"));
		model.addAttribute("iPageSize", mapPage.get("iPageSize"));
		return "webbackground/tourist/OrderCancel";
	}

	/**
	 * 函数功能说明： 跳转--付款页面
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人liys
	 * @date 创建日期 2020.11.17 修改日期 2020.12.28
	 */

	@RequestMapping(value = "/topayorderline")
	public String toPayOrderLine(HttpServletRequest request, Model model) {

		int iType = Integer.parseInt(request.getParameter("type"));
		int iID = Integer.parseInt(request.getParameter("iID"));
		TLineOrder tLineOrder = lineOrderServiceImpl.findByID(iID);
		List<VLineOrder> lstLineOrder = lineOrderServiceImpl.searchReserveGroup(tLineOrder.getReserveName(),
				tLineOrder.getReserveDate(), iType);
		model.addAttribute("tLineOrder", tLineOrder);
		model.addAttribute("lstLineOrder", lstLineOrder);
		List<Map<String, Object>> lstPlanIdName = lineOrderServiceImpl.findPlanIdName(lstLineOrder);
		model.addAttribute("plan", lstPlanIdName);
		int iLineID = Integer.parseInt(request.getParameter("iLineID"));
		model.addAttribute("iLineID", iLineID);
		return "webbackground/tourist/PayOrderLine";
	}

	/**
	 * 函数功能说明： 提交--付款
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @author 创建人 shangzl 修改人 liys
	 * @date 创建日期 2020.11.17 修改日期 2021.01.13
	 */
	@RequestMapping("/savepayorderline")
	public String savePayOrderLine(@RequestParam("lineOrder") String lineOrder, HttpSession session) {
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		List<VLineOrder> lstLineOrder = JSON.parseArray(lineOrder, VLineOrder.class);
		lineOrderServiceImpl.savePayOrderLine(lstLineOrder, tSysUser);
		return "redirect:orderreserved";
	}

	/**
	 * 函数功能说明：跳转--退款页
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 liys
	 * @date 创建日期 2021.01.18
	 */
	@RequestMapping(value = "/tocancelorderline")
	public String toCancelOrderLine(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TLineOrder tLineOrder = lineOrderServiceImpl.findByID(iID);
		model.addAttribute("tLineOrder", tLineOrder);
		int iLineID = Integer.parseInt(request.getParameter("iLineID"));
		model.addAttribute("iLineID", iLineID);
		return "webbackground/tourist/CancelOrderLine";
	}

	/**
	 * 函数功能说明：重定向--处理退款请求
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 liys
	 * @date 创建日期 2021.01.18
	 */
	@RequestMapping("/tocancelorderlineunion")
	public String toCancelOrderLineUnion(HttpServletRequest request, HttpSession session) {

		int iType = Integer.parseInt(request.getParameter("type"));
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		int iID = Integer.parseInt(request.getParameter("iID"));
		int iLineID = Integer.parseInt(request.getParameter("iLineID"));
		int iOrgID = tSysUser.getOrgID();
		VLineOrder vLineOrder = lineOrderServiceImpl.findVByID(iID);
		List<Integer> lstLineID = lineBaseServiceImpl.findByOrgID(iOrgID, iType);
		Integer orderUserOrgID = vLineOrder.getOrderUserOrgID();
		if (iOrgID != orderUserOrgID) {
			return null;
		} else {
			if (lstLineID.contains(iLineID)) {
				return "redirect:tocancelorderline?iID=" + iID + "&iLineID=" + iLineID;
			} else {
				return "redirect:applyrefund?iID=" + iID;
			}
		}
	}

	/**
	 * 函数功能说明：提交--申请退单
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 liys
	 * @date 创建日期 2021.01.18
	 */

	@RequestMapping("/applyrefund")
	public String applyRefund(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TLineOrder tLineOrder = lineOrderServiceImpl.findByID(iID);
		tLineOrder.setRefundProcess((byte) 1);
		lineOrderServiceImpl.save(tLineOrder);
		return "redirect:orderpaid";
	}

	/**
	 * 函数功能说明： 跳转--退款页
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人liys
	 * @date 创建日期 2020.12.29
	 */
	@RequestMapping("/refund")
	public String refund(HttpServletRequest request, Model model) {
		int iID = Integer.parseInt(request.getParameter("iID"));
		TLineOrder tLineOrder = lineOrderServiceImpl.findByID(iID);
		model.addAttribute("tLineOrder", tLineOrder);
		return "webbackground/tourist/CancelOrderLine";
	}

	/**
	 * 函数功能说明： 提交--取消订单
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人 liys
	 * @date 创建日期 修改时间 2021.01.27
	 */
	@RequestMapping("/savecancelorderline")
	public String saveCancelOrderLine(TLineOrder tLineOrder, HttpSession session, String LineID) {

		TLineOrder newLineOrder = lineOrderServiceImpl.findByID(tLineOrder.getID());
		newLineOrder.setRefund(tLineOrder.getRefund());
		newLineOrder.setRefundProcess((byte) 2);
		newLineOrder.setOrderState((byte) 4);
		lineOrderServiceImpl.save(newLineOrder);
		return "redirect:printcancelline?iID=" + tLineOrder.getID() + "&iLineID=" + LineID;
	}

	/**
	 * 函数功能说明： 退单申请
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人liys
	 * @date 创建日期 2020.12.29
	 */
	@RequestMapping("/ordercancelapply")
	public String OrderCancelApply(TLineOrder tLineOrder, HttpSession session, String LineID) throws Exception {

		TLineOrder newLineOrder = lineOrderServiceImpl.findByID(tLineOrder.getID());
		newLineOrder.setOrderState((byte) 4);
		newLineOrder.setRefund(tLineOrder.getRefund());
		lineOrderServiceImpl.save(newLineOrder);
		return "redirect:printcancelline?iID=" + tLineOrder.getID() + "&iLineID=" + LineID;
	}

	/**
	 * 函数功能说明： 取消退单
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人liys
	 * @date 创建日期 2020.12.29
	 */
	@RequestMapping(value = "/cancelrefund")
	public String cancelrefund(HttpServletRequest request) {
		int iID = Integer.parseInt(request.getParameter("iID"));
		TLineOrder tLineOrder = lineOrderServiceImpl.findByID(iID);
		tLineOrder.setRefundProcess((byte) 3);
		tLineOrder.setOrderState((byte) 2);
		lineOrderServiceImpl.save(tLineOrder);
		return "redirect:ordercancel";
	}

	/**
	 * 函数功能说明： 批发允许退单
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人liys
	 * @date 创建日期 2021.01.18
	 */
	@RequestMapping("cancelAllow")
	public String cancelAllow(HttpServletRequest request) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		BigDecimal fRefund = new BigDecimal(request.getParameter("ref"));
		TLineOrder tLineOrder = lineOrderServiceImpl.findByID(iID);
		tLineOrder.setRefundProcess((byte) 2);
		tLineOrder.setRefund(fRefund);
		lineOrderServiceImpl.save(tLineOrder);
		return "redirect:ordercancel";
	}

	/**
	 * 函数功能说明： 批发不允许退单
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人liys
	 * @date 创建日期 2021.01.18
	 */
	@RequestMapping("cancelDisAllow")
	public String cancelDisAllow(HttpServletRequest request) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TLineOrder tLineOrder = lineOrderServiceImpl.findByID(iID);
		tLineOrder.setRefundProcess((byte) 3);
		tLineOrder.setOrderState((byte) 4);
		tLineOrder.setRefund(new BigDecimal(0));
		lineOrderServiceImpl.save(tLineOrder);
		return "redirect:ordercancel";
	}

	/**
	 * 函数功能说明： 跳转--修改线路预留信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人 shangzl
	 * @date 创建日期 修改时间 2020.11.17
	 */

	@RequestMapping(value = "/toupdatereserveline")
	public String toUpdateReserveLine(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TLineOrder tLineOrder = lineOrderServiceImpl.findByID(iID);
		model.addAttribute("tLineOrder", tLineOrder);
		int iLineID = Integer.parseInt(request.getParameter("iLineID"));
		model.addAttribute("iLineID", iLineID);
		return "webbackground/touristrout/UpdateReserveLine";
	}

	/**
	 * 函数功能说明： 提交--修改线路预留信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 liys
	 * @date 创建日期 修改时间 2021.03.24
	 */
	@RequestMapping("/updatereserveline")
	public String updateReserveLine(TLineOrder tLineOrder, HttpSession session, String LineID) throws Exception {

		TLineOrder newLineOrder = lineOrderServiceImpl.findByID(tLineOrder.getID());
		newLineOrder.setReserveName(tLineOrder.getReserveName());
		newLineOrder.setReserveTel(tLineOrder.getReserveTel());
		newLineOrder.setOrderNumber(tLineOrder.getOrderNumber());
		newLineOrder.setMemo(tLineOrder.getMemo());
		newLineOrder.setModifyDate(new Date());
		BigDecimal bPrice = newLineOrder.getPrice();
		int iNumber = tLineOrder.getOrderNumber();
		BigDecimal bNumber = new BigDecimal(iNumber);
		newLineOrder.setOrderAmount(bPrice.multiply(bNumber));
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		newLineOrder.setReviser(tSysUser.getUserName());
		lineOrderServiceImpl.save(newLineOrder);
		return "redirect:orderreserved?LineID=" + Integer.parseInt(LineID);
	}

	/**
	 * 函数功能说明： 提交--取消预留
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人 liys
	 * @date 创建日期 修改时间 2021.01.20
	 */
	@RequestMapping(value = "/tocancelreserveline")
	public String toCancelReserveLine(HttpServletRequest request, Model model) {
		int iID = Integer.parseInt(request.getParameter("iID"));
		TLineOrder tLineOrder = lineOrderServiceImpl.findByID(iID);
		tLineOrder.setOrderState((byte) 3);
		lineOrderServiceImpl.save(tLineOrder);
		return "redirect:orderreserved";
	}

	/**
	 * 函数功能说明： 跳转--修改线路订单信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人shangzl
	 * @date 创建日期 修改时间 2020.11.17
	 */

	@RequestMapping(value = "/toupdateorderline")
	public String toUpdateOrderLine(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TLineOrder tLineOrder = lineOrderServiceImpl.findByID(iID);
		model.addAttribute("tLineOrder", tLineOrder);
		int iLineID = Integer.parseInt(request.getParameter("iLineID"));
		model.addAttribute("iLineID", iLineID);

		return "webbackground/tourist/UpdateOrderLine";

	}

	/**
	 * 函数功能说明： 提交--修改线路订单信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人liys
	 * @date 创建日期 2020.11.17 修改时间 2021.01.27
	 */
	@RequestMapping(value = "/updateorderline")
	public String updateOrderLine(TLineOrder tLineOrder, HttpSession session, String LineID) {

		TLineOrder newLineOrder = lineOrderServiceImpl.findByID(tLineOrder.getID());
		newLineOrder.setOrderName(tLineOrder.getOrderName());
		newLineOrder.setOrderTel(tLineOrder.getOrderTel());
		newLineOrder.setOrderNumber(tLineOrder.getOrderNumber());
		newLineOrder.setMemo(tLineOrder.getMemo());
		newLineOrder.setModifyDate(new Date());
		BigDecimal bPrice = newLineOrder.getPrice();
		int iNumber = tLineOrder.getOrderNumber();
		BigDecimal bNumber = new BigDecimal(iNumber);
		newLineOrder.setOrderAmount(bPrice.multiply(bNumber));
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		newLineOrder.setReviser(tSysUser.getUserName());
		lineOrderServiceImpl.save(newLineOrder);
		return "redirect:orderpaid?LineID=" + Integer.parseInt(LineID);
	}

	/**
	 * 函数功能说明： 提交--删除取消预留信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人 shangzl
	 * @date 创建日期 修改时间 2020.11.17
	 */

	@RequestMapping(value = "/delreserveline")
	public String delReserveLine(String checkList, HttpServletRequest request, Model model) {

		String[] arrChecked = checkList.split(",");
		List<Integer> lstLineID = new ArrayList<>();

		for (String str : arrChecked) {
			lstLineID.add(Integer.parseInt(str));
		}
		lineOrderServiceImpl.delLineOrder(lstLineID);
		int iLineID = Integer.parseInt(request.getParameter("iLineID"));
		model.addAttribute("iLineID", iLineID);
		VLineBase vLineBase = lineBaseServiceImpl.findByLineID(iLineID);
		model.addAttribute("vLineBase", vLineBase);

		List<VLineOrder> lstLineOrder = lineOrderServiceImpl.findByLineID(iLineID);
		model.addAttribute("lstLineOrder", lstLineOrder);

		return "webbackground/tourist/LineBaseInfo";
	}

	/**
	 * 函数功能说明： 提交--删除取消订单信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人 shangzl
	 * @date 创建日期 修改时间 2020.11.17
	 */

	@RequestMapping(value = "/delorderline")
	public String delOrderLine(String checkList, HttpServletRequest request, Model model) {

		String[] arrChecked = checkList.split(",");
		List<Integer> lstLineID = new ArrayList<>();
		for (String str : arrChecked) {
			lstLineID.add(Integer.parseInt(str));
		}
		lineOrderServiceImpl.delLineOrder(lstLineID);
		int iLineID = Integer.parseInt(request.getParameter("iLineID"));
		model.addAttribute("iLineID", iLineID);
		VLineBase vLineBase = lineBaseServiceImpl.findByLineID(iLineID);
		model.addAttribute("vLineBase", vLineBase);
		List<VLineOrder> lstLineOrder = lineOrderServiceImpl.findByLineID(iLineID);
		model.addAttribute("lstLineOrder", lstLineOrder);
		return "webbackground/tourist/LineBaseInfo";
	}

}
